package com.basic.business.declineBacklog.controller;


import com.basic.business.declineBacklog.domain.TDeclineBacklog;
import com.basic.business.declineBacklog.service.ITDeclineBacklogService;
import com.basic.business.pic.service.ITGoodsPicService;
import com.basic.common.annotation.FileExport;
import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.enums.BusinessType;
import com.basic.common.utils.SecurityUtils;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

import static com.basic.common.utils.DateUtils.getNowDate;

/**
 * 下滑预警待办事项Controller
 *
 * @author zhaoxy_jn@163.com
 */
@RestController
@RequestMapping("/business/declineBacklog")
public class TDeclineBacklogController extends BaseController {
    @Resource
    private ITDeclineBacklogService tDeclineBacklogService;

    @Resource
    private ITGoodsPicService tGoodsPicService;

    /**
     * 查询下滑预警待办事项列表
     */
    @PreAuthorize("@ss.hasPermi('business:declineBacklog:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDeclineBacklog tDeclineBacklog) {
        startPage();
        List<TDeclineBacklog> list = tDeclineBacklogService.getList(tDeclineBacklog);
        for (TDeclineBacklog log : list) {
            log.setImgUrl(tGoodsPicService.getPicUrl(log.getGoodsId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出下滑预警待办事项列表
     */
    @PreAuthorize("@ss.hasPermi('business:declineBacklog:export')")
    @Log(title = "下滑预警待办事项", businessType = BusinessType.EXPORT)
    @FileExport(fileName = "下滑预警待办事项.xlsx")
    @PostMapping("/export")
    public String export(TDeclineBacklog tDeclineBacklog) throws IOException {
        List<TDeclineBacklog> list = tDeclineBacklogService.getList(tDeclineBacklog);
        return EasyExcelUtils.excelExport(TDeclineBacklog.class, "下滑预警待办事项数据", list);
    }

    /**
     * 获取下滑预警待办事项详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:declineBacklog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tDeclineBacklogService.get(id));
    }

    /**
     * 新增下滑预警待办事项
     */
    @PreAuthorize("@ss.hasPermi('business:declineBacklog:add')")
    @Log(title = "下滑预警待办事项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDeclineBacklog tDeclineBacklog) {
        tDeclineBacklog.setUserId(SecurityUtils.getUserId());
        tDeclineBacklog.setDeptId(SecurityUtils.getDeptId());
        return toAjax(tDeclineBacklogService.insertBacklog(tDeclineBacklog));
    }

    /**
     * 修改下滑预警待办事项
     */
    @PreAuthorize("@ss.hasPermi('business:declineBacklog:edit')")
    @Log(title = "下滑预警待办事项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDeclineBacklog tDeclineBacklog) {
        return toAjax(tDeclineBacklogService.update(tDeclineBacklog));
    }

    /**
     * 修改链接待办事项优化动作
     */
    @PostMapping("/editOptimizeActions")
    public AjaxResult editOptimizeActions(@RequestBody TDeclineBacklog tDeclineBacklog) {
        //优化时间
        tDeclineBacklog.setOptimizeTime(getNowDate());

        Instant start = tDeclineBacklog.getCreateTime().toInstant();
        Instant end = tDeclineBacklog.getOptimizeTime().toInstant();

        // 计算时间差
        Duration duration = Duration.between(start, end);

        double hour = duration.toMinutes() / 60.0;

        String formatted = String.format("%.2f", hour);

        tDeclineBacklog.setOptimizationDuration(Double.parseDouble(formatted));

        //已优化
        tDeclineBacklog.setOptimizeStatus(1);

        return toAjax(tDeclineBacklogService.update(tDeclineBacklog));
    }

    /**
     * 删除下滑预警待办事项
     */
    @PreAuthorize("@ss.hasPermi('business:declineBacklog:remove')")
    @Log(title = "下滑预警待办事项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tDeclineBacklogService.deleteByIds(ids));
    }
}
